<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 代理+ 反射
        let formData = {
            account: 'admin',
            password: '123456',
            email: "2478774277@qq.com",
            phone: "18339310992"
        };
        // 账号：6~20
        // 密码：6~20
        // 作业1：账号\密码至少含有字母+数字
        var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/;
        var emai = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
        var tel = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/
            // console.log(reg.test("abcaaasda"));
        let handel = {
            set: function(target, key, value) {
                //key要准备修改的属性名称
                //判断为账号acocunt，密码password
                if (key == 'account' || key == 'password') {
                    if (!reg.test(value)) {
                        return;
                    }
                }
                //判断为邮箱email
                if (key == 'email') {
                    if (!emai.test(value)) {
                        return;
                    }
                }
                //判断为手机号phone
                if (key == 'phone') {
                    if (!tel.test(value)) {
                        return;
                    }
                }
                target[key] = value;
            },
            get: function(target, key) {
                // console.log('get函数被调用：', target, key)
                return target[key];
            }
        }
        formData = new Proxy(formData, handel);
        Reflect.set(formData, 'account', 'abcd123456');
        Reflect.set(formData, 'password', 'admin123');
        Reflect.set(formData, 'email', '2478774277@qq.com');
        Reflect.set(formData, 'phone', '18339310992');
        console.log(formData);
        let account = Reflect.get(formData, 'account');
        let password = Reflect.get(formData, 'password');
        let email = Reflect.get(formData, 'email');
        let phone = Reflect.get(formData, 'phone');
        console.log(account, password, email, phone);
    </script>
</body>

</html>